#include<iostream>
#include<chrono>
#include<fstream>
#include"Vector.h"
#define RESIZE_MAX 10 //resize次数
#define TEST_MAX 1000 //重复测试的次数
#define VECTOR_MAX 100 //生成的Vector数

using namespace std;

void test(string outname, int TESTSIZE);//output the result of TESTSIZE to the file with a certain name
inline int rand_N(int testsize)
{
    return ((int)((float)rand() / (float)RAND_MAX * testsize));
}

int main ()
{
    test("result1024.csv",1024);
    test("result512.csv", 512);
    test("result256.csv",256);
    test("result128.csv", 128);
    return 0;
}

void test(string outname, const int TESTSIZE)
{
    // ofstream out("result.txt");
    ofstream csv(outname);
    csv << "size,time,capacity" << endl;
    for (int i = 1;i<=TESTSIZE;i++)
    {
        Vector<double>::SPARE_CAPACITY = i * sizeof(double);
        int Cap_Count = 0;
        auto start = std::chrono::high_resolution_clock::now();
        for (int l = 0; l < TEST_MAX;l++)
        {
            for (int j = 0;j<VECTOR_MAX;j++)
            {
                int size = rand_N(TESTSIZE);
                Vector<double> a(size);
                for (int k = 0;k<RESIZE_MAX;k++)
                {
                    size = rand_N(TESTSIZE);
                    a.resize(size);
                }
                Cap_Count += a.capacity();
            }
        }
        auto end = std::chrono::high_resolution_clock::now();
        auto elapsed = end -start;
        cout << "The size of SPARE_CAPACITY: " << i << endl;
        cout << "Total time: " << elapsed.count()/(double)TEST_MAX  << endl;
        cout << "Total capacity: " << Cap_Count/(double)TEST_MAX << endl;
        std::cout << "------------------------------------" << std::endl;
        // out << "The size of SPARE_CAPACITY: " << i << endl;
        // out << "Total time: " << elapsed.count() /(double)TEST_MAX  << endl;
        // out << "Total capacity: " << Cap_Count /(double)TEST_MAX << endl;
        // out << "------------------------------------" << std::endl;
        csv << i << "," << elapsed.count() / (double)TEST_MAX << "," << Cap_Count / (double)TEST_MAX << endl;
    }
}